<?php

namespace app\common\repositories\piles;

use think\facade\Db;
use app\common\dao\piles\PilesFaultyLogDao;
use app\common\repositories\BaseRepository;
use app\common\repositories\property\users\PropertyUsersLogRepository;

/**
 * Class PilesFaultyLogRepository
 *
 * @mixin PilesFaultyLogDao
 */
class PilesFaultyLogRepository extends BaseRepository
{

    public function __construct(PilesFaultyLogDao $dao)
    {
        $this->dao = $dao;
    }

    public function getPropertyList(array $where, $page, $limit, int $propertyId = null)
    {
        $query = $this->dao->search($where, $propertyId);
        $count = $query->count();
        $list = $query->page($page, $limit)
            ->order('id', 'desc')
            ->with([
                'pilesInfo' => function ($query) {
                    $query->field('id,piles_name,sim_key');
                    $query->bind(['piles_name', 'sim_key']);
                },
            ])
            ->select();
        return compact('list', 'count');
    }

    public function addInfo(int $pilesId, int $userId, array $datas = null, $faultMsg = null)
    {
        $data['user_id'] = $userId;
        $data['piles_id'] = $pilesId;
        if (isset($datas['fault_code'])) {
            $data['fault_code'] = $datas['fault_code'];
        }
        if (isset($datas['order_id'])) {
            $data['order_id'] = $datas['order_id'];
        }
        if (isset($datas['property_id'])) {
            $data['property_id'] = $datas['property_id'];
        }
        if (isset($datas['property_id'])) {
            $data['property_id'] = $datas['property_id'];
        }
        if (isset($faultMsg)) {
            $data['fault_msg'] = $faultMsg;
        }
        $this->dao->create($data);
    }

    public function getInfo($data)
    {
        return $this->dao->getSearch($data)->find();
    }

    public function handleInfo($info, array $data, int $userId = 0)
    {
        return Db::transaction(function () use ($data, $info, $userId) {
            /** @var PropertyUsersLogRepository $repository */
            $repository = app()->make(PropertyUsersLogRepository::class);
            $repository->addInfo($info['property_id'], $userId, 'PILES.FAULTY.HANDLE', $info);
            return $this->dao->update($info['id'], $data);
        });
    }

    public function batchDelete(array $ids, int $userId = 0)
    {
        $list = $this->dao->selectWhere([
            ['id', 'in', $ids]
        ]);
        if ($list) {
            foreach ($list as $k => $v) {
                $this->deleteInfo($v, $userId);
            }
            return $list;
        }
        return [];
    }

    public function deleteInfo($info, int $userId = 0)
    {
        return Db::transaction(function () use ($info, $userId) {
            /** @var PropertyUsersLogRepository $repository */
            $repository = app()->make(PropertyUsersLogRepository::class);
            $repository->addInfo($info['property_id'], $userId, 'PILES.FAULTY.DEL', $info);
            return $this->dao->delete($info['id']);
        });
    }
}
